//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    songlist: [],
    songname: ''
  },

  onLoad: function () {
  },
  input: function(e) {
    this.setData({
      songname: e.detail.value
    })
  },
  navPlay: function(e){
    wx.navigateTo({
      url: '/pages/play/play?filehash=' + e.target.dataset.filehash
      + "&singer=" + e.target.dataset.singer,
    })
  },
  getSongList: function() {
    if(this.data.songname.trim() == '') {
      wx.showToast({
        title: '请输入歌曲名称',
        image: '/images/cancle.png'
      })
      return
    }

    wx.showLoading({
      title: '正在查询......',
    })
    wx.request({
      url: 'https://music.********.com/getSongList',
      data: { songname: this.data.songname.trim()},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        let list =[];
        let a = res.data.res;
        for (let i = 0; i < a.length; i++) {
          let text  = a[i].FileName.replace("<em>", "").replace("</em>", "");
          let sec = a[i].Duration % 60 < 10 ? '0' + a[i].Duration % 60 : a[i].Duration % 60;
          let time = parseInt(a[i].Duration / 60) + ':' + sec ;
          list.push({
            FileName: text,
            Duration: time,
            FileHash: a[i].FileHash,
            SingerName: a[i].SingerName
          })
        }
        this.setData({
          songlist: list
        })
      },
      fail: function () {

      },
      complete: function () {
        wx.hideLoading()
      }
    })
  }
})
